@extends('steward.layoutList')
@section('css')
    <style>
        .col-extend-css {
            width:100%;
            text-align: left;
        }
    </style>
@endsection
@section('title')
    <li class="cur">
        <span>价格列表</span>
    </li>
@endsection

@section('search')
    <div class="form-group" >
        <label for="product_code">商家编码：</label>
        <input type="text" placeholder="请输入商家编码" class="form-control" name="product_code" id="product_code">&nbsp;&nbsp;
    </div>
    <div class="form-group" >
        <label for="goods_name">商品名称：</label>
        <input type="text" placeholder="请输入商品名称" class="form-control" name="goodsName" id="goodsName">
    </div>
    <div style="height:15px;"></div>

    <div class="form-group" >
        <label >价格范围：</label>
        <input type="text" placeholder="请输入开始价格" class="form-control" name="min_price" id="min_price" onblur="goods.checkPrice();"> 至
        <input type="text" placeholder="请输入结束价格" class="form-control" name="max_price" id="max_price" onblur="goods.checkPrice();">
    </div>
@endsection

@section('js')
    <script>
    var bootstrap_table_ajax_url = '/lar/steward/price/search/';
    bootstrap_table({
    sortName : 'wms_goods_master.timeStamp',
    sortOrder : 'desc',
    columns: [ //字段
    { title: '商品名称',  field: 'goodsName', align: 'left' },
    { title: '商家编码', field: 'code_info', align: 'left', width:'150px' },
    { title: '库存规格', field: 'spec', align: 'left', width:'100px' },
    { title: '价格', field: 'salePrice', align: 'left', width:'100px' },
    { title: '上次修改时间', field: 'timeStamp', align: 'left', width:'100px' }
    ]
    });

    //搜索条件的判断
    var goods = {

        checkPrice : function () {

            var price_min = $('#min_price').val();
            var price_max = $('#max_price').val();

            if(price_min && !E.isNum(price_min)){
                layer.msg('请输入正确的价格',{icon:2,shade: [0.15, 'black'],offset:'120px',time:2000});
                return false;
            }
            if(price_max && !E.isNum(price_max)){
                layer.msg('请输入正确的价格',{icon:2,shade: [0.15, 'black'],offset:'120px',time:2000});
                return false;
            }
            if(price_min && price_max && parseFloat(price_min) > parseFloat(price_max)){
                layer.msg('开始价格不能大于结束价格',{icon:2,shade: [0.15, 'black'],offset:'120px',time:2000});
            }
        }
    };

        //修改
    $(document).on('click','.price',function(){
        var _this=$(this);
        var product_code= $(this).attr('data_id');
        var price=$(this).attr('data_type');
        var html='<div id="pop" style="margin-top:10px;" style="width: 100%"> ' +
                '<div style="background: #ffffff">' +
                ' <form id="pop_form" onsubmit="return false;" class="form-horizontal" role="form"> ' +
                '<div class="form-group" style="margin-right: 1px; margin-left: 1px;"> ' +
                '<label class="col-sm-3 control-label" for="price_edit" >' +
                '<span style="color:red">* </span>价格：</label>' +
                '<div class="col-sm-8">' +
                '<input class="form-control"  style="width: 200px;" type="text" id="price_edit" name="price_edit" maxlength="100" value="'+price+'" />' +
                '</div></div>' +
                '</form></div></div>';
        if( E.isInt( product_code )){
            layer.open({
                title: '价格修改',
                type: 1,
                area: ['400px', '200px'],
                content: html,
                btn:['修改','关闭'],
                yes: function(index, layero){
                    var new_price=$('#price_edit').val();
                    if(new_price=='')
                    {
                        err_msg='价格不得为空';
                        layer.msg( err_msg , {icon: 2,shade: [0.15, 'black'],offset:'120px',time:1000});
                        return false;
                    };

                    if( new_price < 0)
                    {
                        err_msg='价格必须是正数';
                        layer.msg( err_msg , {icon: 2,shade: [0.15, 'black'],offset:'120px',time:1000});
                        return false;
                    };
                    if(/^\d+(\.\d{1,2})?$/.test(new_price)){
                        $.ajax({
                            type: "get",
                            dataType: "json",
                            url: '/lar/steward/price/update',
                            data: {
                                product_code:product_code,
                                price:new_price
                            },
                            success: function (msg) {
                                if (msg.code==200) {
                                    layer.closeAll();
                                    layer.msg( msg.message , {icon: 1,shade: [0.15, 'black'],offset:'120px',time:1000});
                                    var data='&nbsp;'+'<a href="javascript:void(0)" class="price" data_type="'+new_price+'" data_id="'+product_code+ '"><span class="glyphicon glyphicon-pencil"></span></a>';
                                   $("#"+product_code).html(new_price+data);
                                     _this.attr('data_type',new_price);
                                    var time=msg.data.date.substr(0, 19);
                                     $('#t_'+product_code).html(time);
                                }else{
                                    layer.msg( msg.message , {icon: 2,shade: [0.15, 'black'],offset:'120px',time:1000});
                                };
                            }
                        });
                    }else{
                        err_msg='价格必须是带两位小数点的数字';
                        layer.msg( err_msg , {icon: 2,shade: [0.15, 'black'],offset:'120px',time:1000});
                        return false;
                    }
                }
                ,btn2: function(index, layero){
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                }
            })
        }
    });
    </script>
@endsection
